{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7055bf43",
   "metadata": {},
   "source": [
    "### 数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7551b115",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   1,    2,    3,  127, -128], dtype=int8)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "256"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  2, -5,  8, -1,  0], dtype=int8)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.array([1,2,3,127,128],dtype=np.int8)#位数越大，占的内存也大\n",
    "display(arr1,2**8)# **是米运算，2的8次幂，数字范围；-128到127，中间包含0\n",
    "\n",
    "arr2 = np.array([1,2,-5,8,255,256],dtype=np.int8)#无符号的8位表示数字，没有符号\n",
    "display(arr2)# 0-255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0cc5a85c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上面会变成 -128，因为超过了int的数字范围，所以就不行了\n",
    "# 同理， 255，256 打印后就会变成，-1和0.\n",
    "# 小知识，为什么255会变成-1，256变成0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2048277c",
   "metadata": {},
   "source": [
    "8位整数可以表示的值范围是-128到+127。当数组中的元素值超出这个范围时，会发生整数溢出，即最高有效位会被丢弃，只保留最低的8位（1字节），这可能导致表示的值发生变化。\n",
    "\n",
    "在数组中，255的二进制表示为11111111，它在8位整数中的表示方式是-1，因为最高有效位是1，意味着这是一个负数。类似地，256的二进制表示为100000000，但由于它不能用8位整数表示，因此会发生溢出，并保留了最低的8位，即00000000，这被解释为0。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5d5a6e64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2, 251,   8, 255,   0], dtype=uint8)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.array([1,2,-5,8,255,256],dtype=np.uint8)\n",
    "display(arr3)\n",
    "#同上，越界移溢出\n",
    "# 0-255"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd90f5e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[134,  57,  43],\n",
       "       [  4, 133,  65],\n",
       "       [134, 124,  13],\n",
       "       ...,\n",
       "       [ 54,   6,  69],\n",
       "       [ 29,  49,  25],\n",
       "       [ 80,  92,  43]], dtype=int16)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[134,  57,  43],\n",
       "       [  4, 133,  65],\n",
       "       [134, 124,  13],\n",
       "       ...,\n",
       "       [ 54,   6,  69],\n",
       "       [ 29,  49,  25],\n",
       "       [ 80,  92,  43]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0,150,size = (10000,3),dtype=np.int16)\n",
    "arr2 = arr1.astype(np.int64)\n",
    "display(arr1,arr2)\n",
    "#虽然打印出来的数字一样，但是在电脑中占的内存不一样\n",
    "#64所占的内存是16的四倍"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cb1af3c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.06507675,  0.14297543,  1.47847468,  0.16963733, -0.14731854,\n",
       "        0.32518171, -2.27218738, -0.3730286 ,  0.56283709,  1.33834177,\n",
       "       -0.09573432, -0.1772638 ,  0.62559524,  0.02024542, -0.52312678,\n",
       "        2.02398136, -1.19782248, -0.51088909, -1.5591118 ,  0.91149772,\n",
       "       -0.71037719,  0.27554176, -0.89158023, -1.84307923,  0.24986975,\n",
       "        1.10170159, -0.09756599,  0.43074352,  0.86079085,  1.18713547,\n",
       "       -0.46008304,  1.76717227, -1.1708877 , -0.79148839,  1.9597641 ,\n",
       "       -2.52052794,  0.7757333 ,  2.49295291, -1.36352495, -0.23308048,\n",
       "        0.29834195,  0.68487592,  0.37914906, -1.11133985, -1.343791  ,\n",
       "       -0.17531721, -1.29810275, -1.76659431, -0.7204521 , -0.06137495,\n",
       "        1.93332009,  1.69966383, -0.42764334,  0.60431855,  0.84627167,\n",
       "       -2.42729504, -0.3007764 , -0.02344746,  1.08194799, -0.69629646,\n",
       "        0.06835717, -0.44395405, -0.77504631, -0.95837504, -0.68006446,\n",
       "        0.12533349,  0.03047407, -0.78619167, -0.92167377, -1.72035981,\n",
       "       -1.7138903 , -1.19363483, -0.12813022,  0.8088707 , -0.92464286,\n",
       "        0.79787479,  1.38790296,  0.58668143,  0.28171315, -1.68088455,\n",
       "        0.08321822,  0.45495005, -2.11709372, -1.8876178 , -0.470784  ,\n",
       "       -0.20823554, -1.35365719, -1.02788689, -0.35424097, -0.78485869,\n",
       "       -1.48211132, -0.52343227,  0.55156485,  0.4046964 , -0.51047332,\n",
       "       -0.96836869, -0.43770349,  0.62921295, -0.27935477,  1.15010896])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-0.06506,  0.143  ,  1.479  ,  0.1697 , -0.1473 ,  0.3252 ,\n",
       "       -2.271  , -0.373  ,  0.563  ,  1.338  , -0.09576, -0.1772 ,\n",
       "        0.6255 ,  0.02025, -0.523  ,  2.023  , -1.198  , -0.5107 ,\n",
       "       -1.56   ,  0.9116 , -0.7104 ,  0.2756 , -0.8916 , -1.843  ,\n",
       "        0.2499 ,  1.102  , -0.0976 ,  0.4307 ,  0.861  ,  1.1875 ,\n",
       "       -0.4602 ,  1.768  , -1.171  , -0.7915 ,  1.96   , -2.521  ,\n",
       "        0.776  ,  2.492  , -1.363  , -0.233  ,  0.2983 ,  0.685  ,\n",
       "        0.3792 , -1.111  , -1.344  , -0.1753 , -1.298  , -1.767  ,\n",
       "       -0.72   , -0.06137,  1.934  ,  1.699  , -0.4277 ,  0.6045 ,\n",
       "        0.846  , -2.428  , -0.3008 , -0.02345,  1.082  , -0.6963 ,\n",
       "        0.06836, -0.4438 , -0.775  , -0.9585 , -0.68   ,  0.1254 ,\n",
       "        0.03047, -0.786  , -0.922  , -1.721  , -1.714  , -1.193  ,\n",
       "       -0.1282 ,  0.809  , -0.925  ,  0.798  ,  1.388  ,  0.587  ,\n",
       "        0.2817 , -1.681  ,  0.0832 ,  0.4548 , -2.117  , -1.888  ,\n",
       "       -0.4707 , -0.2083 , -1.354  , -1.028  , -0.3542 , -0.7847 ,\n",
       "       -1.482  , -0.5234 ,  0.552  ,  0.4048 , -0.5103 , -0.9683 ,\n",
       "       -0.4377 ,  0.6294 , -0.2793 ,  1.15   ], dtype=float16)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('float16')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr3 = np.random.randn(100)\n",
    "#每个元素都是从标准正态分布(均值为0，标准差为1)中独立随机抽取的\n",
    "#正态分布的特征\n",
    "display(arr3,arr3.dtype)\n",
    "arr4 = arr3.astype(np.float16)\n",
    "display(arr4,arr4.dtype)\n",
    "#通过下图发现，16位的数字会更加精准"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cea74b9",
   "metadata": {},
   "source": [
    "### 数组运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba380e9f",
   "metadata": {},
   "source": [
    "#### 普通运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8bc4b6f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 7, 4, 2],\n",
       "       [5, 9, 0, 3, 3],\n",
       "       [3, 9, 0, 8, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ 4,  3,  8,  5,  3],\n",
       "       [ 6, 10,  1,  4,  4],\n",
       "       [ 4, 10,  1,  9,  3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-2, -3,  2, -1, -3],\n",
       "       [ 0,  4, -5, -2, -2],\n",
       "       [-2,  4, -5,  3, -3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0,10,size = (3,5))\n",
    "display(arr1,arr1+1,arr1-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b0c2da5a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9,  4, 49, 16,  4],\n",
       "       [25, 81,  0,  9,  9],\n",
       "       [ 9, 81,  0, 64,  4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 幂运算\n",
    "display(arr1**2,(arr1**0.5).dtype) # dtype是看它的类型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "389b89cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.66666667, 2.33333333, 1.33333333, 0.66666667],\n",
       "       [1.66666667, 3.        , 0.        , 1.        , 1.        ],\n",
       "       [1.        , 3.        , 0.        , 2.66666667, 0.66666667]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b9696f4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上面的数字整数后面都会有一个。.，这就是说明是float，是64位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "51ce1d99",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 7, 4, 2],\n",
       "       [5, 9, 0, 3, 3],\n",
       "       [3, 9, 0, 8, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 3, 2, 1],\n",
       "       [2, 4, 0, 1, 1],\n",
       "       [1, 4, 0, 4, 1]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 整除，模运算\n",
    "display(arr1,arr1//2,(arr1%3).dtype)\n",
    "# arr1&3 会出现几个余数呢，3个， 1，2，3，"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c873171",
   "metadata": {},
   "source": [
    "#### 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "193df60b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 7, 4, 2],\n",
       "       [5, 9, 0, 3, 3],\n",
       "       [3, 9, 0, 8, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ True, False, False, False, False],\n",
       "       [False, False, False,  True,  True],\n",
       "       [ True, False, False, False, False]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ True,  True, False,  True,  True],\n",
       "       [False, False,  True,  True,  True],\n",
       "       [ True, False,  True, False,  True]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1,arr1 == 3,arr1<5)\n",
    "#在这个逻辑运算里面，返回的是布尔类型\n",
    "#是3 就是True，其余为False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d5340129",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True, False, False,  True, False],\n",
       "       [False, False, False,  True,  True],\n",
       "       [ True, False, False, False, False]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display((arr1 > 2) & (arr1<=4)) #大于2并且小于等于4\n",
    "# 与运算，左右两边都满足才可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2c8dc8b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True, False, False,  True],\n",
       "       [False, False,  True, False, False],\n",
       "       [False, False,  True, False,  True]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(~(arr1 >2))\n",
    "# ~这个为非运算，就是原来的True变成False，False变成True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "64a8f103",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False],\n",
       "       [False,  True, False, False, False],\n",
       "       [False,  True, False, False, False]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display((arr1 == 1)|(arr1 == 9))\n",
    "# 或运算，左边满足可以，右边满足也行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fbe8f2f",
   "metadata": {},
   "source": [
    "#### *=，+=，/=，-="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "46b462f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 7, 4, 2],\n",
       "       [5, 9, 0, 3, 3],\n",
       "       [3, 9, 0, 8, 2]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5d0f6645",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ -7,  -8,  -3,  -6,  -8],\n",
       "       [ -5,  -1, -10,  -7,  -7],\n",
       "       [ -7,  -1, -10,  -2,  -8]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 -= 10\n",
    "arr1\n",
    "#修改原数据。所有数据都-10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "83fc7186",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[13, 12, 17, 14, 12],\n",
       "       [15, 19, 10, 13, 13],\n",
       "       [13, 19, 10, 18, 12]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 +=20\n",
    "arr1\n",
    "#在这个基础又加了20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "84d2e384",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[26, 24, 34, 28, 24],\n",
       "       [30, 38, 20, 26, 26],\n",
       "       [26, 38, 20, 36, 24]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 *=2\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "b5ee93c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[26, 24, 34, 28, 24],\n",
       "       [30, 38, 20, 26, 26],\n",
       "       [26, 38, 20, 36, 24]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[26., 24., 34., 28., 24.],\n",
       "       [30., 38., 20., 26., 26.],\n",
       "       [26., 38., 20., 36., 24.]], dtype=float32)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[6.5, 6. , 8.5, 7. , 6. ],\n",
       "       [7.5, 9.5, 5. , 6.5, 6.5],\n",
       "       [6.5, 9.5, 5. , 9. , 6. ]], dtype=float32)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 原数据类型 int\n",
    "# 转变数据类型就不会报错了\n",
    "arr2 =arr1.astype(np.float32)\n",
    "display(arr1,arr2)\n",
    "arr2 /=4\n",
    "arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2484ee4a",
   "metadata": {},
   "source": [
    "#### 数组和数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d7ed4673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[26, 24, 34, 28, 24],\n",
       "       [30, 38, 20, 26, 26],\n",
       "       [26, 38, 20, 36, 24]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1\n",
    "arr3 = np.array([1,2,3,4,5])\n",
    "display(arr1,arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7a5d56a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[27, 26, 37, 32, 29],\n",
       "       [31, 40, 23, 30, 31],\n",
       "       [27, 40, 23, 40, 29]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr3 #广播机制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d87e6b01",
   "metadata": {},
   "source": [
    "### 复制和视图"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0229e27b",
   "metadata": {},
   "source": [
    "#### 完全没有复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d55a9083",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[29, 79, 94, 82, 84],\n",
       "       [70, 93, 18, 44, 24],\n",
       "       [75, 67, 75, 46,  3],\n",
       "       [21, 60, 30, 18, 96]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[7630,   79,   94,   82,   84],\n",
       "       [  70,   93,   18,   44,   24],\n",
       "       [  75,   67,   75,   46,    3],\n",
       "       [  21,   60,   30,   18,   96]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[7630,   79,   94,   82,   84],\n",
       "       [  70,   93,   18,   44,   24],\n",
       "       [  75,   67,   75,   46,    3],\n",
       "       [  21,   60,   30,   18,   96]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a= np.random.randint(0,100,size = [4,5])\n",
    "b=a\n",
    "display(b)\n",
    "print(a is b)\n",
    "\n",
    "b[0,0]= 7630\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "57e059fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ddd09f8",
   "metadata": {},
   "source": [
    "#### 深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0441c535",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[31, 62, 24, 66, 63],\n",
       "       [73, 98, 64, 64, 51],\n",
       "       [81, 81, 45, 57, 16],\n",
       "       [29, 49, 10, 15, 91]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[31, 62, 24, 66, 63],\n",
       "       [73, 98, 64, 64, 51],\n",
       "       [81, 81, 45, 57, 16],\n",
       "       [29, 49, 10, 15, 91]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,   62,   24,   66,   63],\n",
       "       [  73,   98,   64,   64,   51],\n",
       "       [  81,   81,   45,   57,   16],\n",
       "       [  29,   49,   10,   15,   91]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,100, size = (4,5))\n",
    "display(a)\n",
    "b = a.copy() #两者就不同了\n",
    "print(b is a)# 返回False\n",
    "\n",
    "#b的内存指向，是否相同\n",
    "print(b.base is a)\n",
    "\n",
    "#变量数据，是否属于它自己\n",
    "#b.flags 获得变量的自身信息,owndata:判断数据是否是自己的\n",
    "# 属于True 不属于 False\n",
    "print(b.flags.owndata)\n",
    "\n",
    "print(a.flags.owndata)\n",
    "b[0,0] = 1024\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baadb7c0",
   "metadata": {},
   "source": [
    "#### 浅拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7978bae9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[23, 68, 36, 90, 78],\n",
       "       [38,  2, 61, 15, 62],\n",
       "       [99, 10, 48, 25, 42],\n",
       "       [71, 55, 63, 25, 29]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,   68,   36,   90,   78],\n",
       "       [  38,    2,   61,   15,   62],\n",
       "       [  99,   10,   48,   25,   42],\n",
       "       [  71,   55,   63,   25,   29]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1024,   68,   36,   90,   78],\n",
       "       [  38,    2,   61,   15,   62],\n",
       "       [  99,   10,   48,   25,   42],\n",
       "       [  71,   55,   63,   25,   29]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "a = np.random.randint(0,100, size = (4,5))\n",
    "display(a)\n",
    "b = a.view() #使用a中的数据创建一个新数组对象\n",
    "print(a is b)# 返回False,a和b是两个不同名字对应同一个内存对象\n",
    "\n",
    "#b的内存指向，是否相同\n",
    "print(b.base is a)\n",
    "\n",
    "#变量数据，是否属于它自己\n",
    "#b.flags 获得变量的自身信息,owndata:判断数据是否是自己的\n",
    "# 属于True 不属于 False\n",
    "print(b.flags.owndata)\n",
    "\n",
    "print(a.flags.owndata)\n",
    "b[0,0] = 1024\n",
    "display(a,b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ce83d8f",
   "metadata": {},
   "source": [
    "### 索引，切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f8a5459",
   "metadata": {},
   "source": [
    "#### 深拷贝应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "37667f04",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e7152fa3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.0000000e+00, 1.0000000e+00, 2.0000000e+00, ..., 9.9999997e+07,\n",
       "       9.9999998e+07, 9.9999999e+07])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([       0.,  1000000.,  2000000.,  3000000.,  4000000.,  5000000.,\n",
       "        6000000.,  7000000.,  8000000.,  9000000., 10000000., 11000000.,\n",
       "       12000000., 13000000., 14000000., 15000000., 16000000., 17000000.,\n",
       "       18000000., 19000000., 20000000., 21000000., 22000000., 23000000.,\n",
       "       24000000., 25000000., 26000000., 27000000., 28000000., 29000000.,\n",
       "       30000000., 31000000., 32000000., 33000000., 34000000., 35000000.,\n",
       "       36000000., 37000000., 38000000., 39000000., 40000000., 41000000.,\n",
       "       42000000., 43000000., 44000000., 45000000., 46000000., 47000000.,\n",
       "       48000000., 49000000., 50000000., 51000000., 52000000., 53000000.,\n",
       "       54000000., 55000000., 56000000., 57000000., 58000000., 59000000.,\n",
       "       60000000., 61000000., 62000000., 63000000., 64000000., 65000000.,\n",
       "       66000000., 67000000., 68000000., 69000000., 70000000., 71000000.,\n",
       "       72000000., 73000000., 74000000., 75000000., 76000000., 77000000.,\n",
       "       78000000., 79000000., 80000000., 81000000., 82000000., 83000000.,\n",
       "       84000000., 85000000., 86000000., 87000000., 88000000., 89000000.,\n",
       "       90000000., 91000000., 92000000., 93000000., 94000000., 95000000.,\n",
       "       96000000., 97000000., 98000000., 99000000.])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(100,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import gc #垃圾回收\n",
    "a = np.arange(1e8)\n",
    "display(a)\n",
    "b = a[::1000000].copy()#每100万个数据中提取一个数据\n",
    "del a #不需要a，删除占大内存的a\n",
    "gc.collect()\n",
    "display(b,b.shape)\n",
    "#由于每100万个数据中提取一个数据，所以打印出来就是\n",
    "#0-1百万-2百万-3百万，一次一百万的向后涨"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04e52f2d",
   "metadata": {},
   "source": [
    "#### 基本的索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a476c412",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [20, 10, 60, 11],\n",
       "       [ 6, 93, 54, 95],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,100,size = (5,4))\n",
    "display(arr2)\n",
    "#索引都是从0开始的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "40416eac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "89"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c281f8cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "09850e7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果倒着数的话，会从 87的 -1 开始\n",
    "arr2[-1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9932920d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([65, 11])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取两个数字\n",
    "arr2[[0,2],[1,3]]\n",
    "# arr2 [0,[0,1]]\n",
    "# 直接写0也可以的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0518d707",
   "metadata": {},
   "source": [
    "切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bb9fc85d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [20, 10, 60, 11],\n",
       "       [ 6, 93, 54, 95],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[83,  8, 66,  0],\n",
       "       [20, 10, 60, 11]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 左闭右开\n",
    "display(arr2,arr2[1:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9fb29781",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 10],\n",
       "       [ 6, 93]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[2:4,0:2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4309f6f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 11],\n",
       "       [ 6, 95]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[2:4,[0,3]]\n",
    "#和上面不同，这个里面还有一个[]号，代表的是列\n",
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7c93a05d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [20, 10, 60, 11],\n",
       "       [ 6, 93, 54, 95],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [20, 10, 60, 11],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[83,  8, 66,  0],\n",
       "       [ 6, 93, 54, 95]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr2,arr2[::2],arr2[1::2])\n",
    "#::2表示 两个中取一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "666cb2ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e45b6db0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 59],\n",
       "       [ 6, 95]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[[0,3]][:,[0,3]]\n",
    "#中间 ：分号如果不写东西的话就默认取所有！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e3c0c00d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[65, 59],\n",
       "       [10, 11],\n",
       "       [51, 34]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[::2,1::2]\n",
    "#奇数行，偶数列\n",
    "#在这里面是 从索引1开始，每两个中取一个。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "326c2dce",
   "metadata": {},
   "source": [
    "#### 花式索引和索引技巧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ff99aa6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [20, 10, 60, 11],\n",
       "       [ 6, 93, 54, 95],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [89, 65, 87, 59],\n",
       "       [20, 10, 60, 11],\n",
       "       [89, 65, 87, 59]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr2,arr2[[0,1,0,2,0]])\n",
    "#可以重复多次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "5358ecd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 65, 87, 59],\n",
       "       [83,  8, 66,  0],\n",
       "       [20, 10, 60, 11],\n",
       "       [ 6, 93, 54, 95],\n",
       "       [57, 51,  9, 34]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "6bb5594c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[65, 59, 59],\n",
       "       [93, 95, 95],\n",
       "       [93, 95, 95],\n",
       "       [93, 95, 95]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取第四行\n",
    "arr2[[0,3,3,3]][:,[1,3,3]]\n",
    "#这样输入的话，相当于第四行取了三次\n",
    "#根据基数行，偶数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "13eca605",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 8,  0,  0],\n",
       "       [93, 95, 95],\n",
       "       [93, 95, 95],\n",
       "       [93, 95, 95]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2[np.ix_([1,3,3,3],[1,3,3])]\n",
    "# np.ix 可以进行一个交叉\n",
    "#从一维变成了二维\n",
    "#arr[[0,3,3,3],[1,3,3]]\n",
    "#如果逗号前面给行，逗号后面给列，这样直接报错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1980d0f3",
   "metadata": {},
   "source": [
    "boolean 类型的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e0af8831",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 28, 31, 94, 28, 33, 19, 65, 74, 91, 40, 88, 14, 61, 54, 20, 52,\n",
       "       60, 83, 32,  8,  8, 95, 23, 55, 51, 49, 23, 37, 75, 86, 95, 19, 13,\n",
       "       49, 79, 76, 78, 43, 34, 48, 19, 49,  5,  7, 92, 55,  4, 15, 63, 72,\n",
       "       86, 54, 55, 69, 90, 74, 36, 82])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0,100, size = (59))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "73b8ff72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False, False, False, False, False,\n",
       "        True, False,  True, False, False, False, False, False, False,\n",
       "        True, False, False, False,  True, False, False, False, False,\n",
       "       False, False, False,  True,  True, False, False, False, False,\n",
       "       False, False, False, False, False, False, False, False, False,\n",
       "        True, False, False, False, False, False,  True, False, False,\n",
       "       False,  True, False, False,  True])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond = arr1 >80\n",
    "cond"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ab8bb763",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 94, 91, 88, 83, 95, 86, 95, 92, 86, 90, 82])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "5552d0d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False, False, False, False, False,\n",
       "        True, False, False, False, False, False, False, False, False,\n",
       "       False, False,  True,  True,  True, False, False, False, False,\n",
       "       False, False, False, False,  True, False, False, False, False,\n",
       "       False, False, False, False, False, False, False,  True,  True,\n",
       "        True, False,  True, False, False, False, False, False, False,\n",
       "       False, False, False, False, False])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond2 = (arr1 <10)|(arr1 >90)\n",
    "cond2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "a73d78b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 94, 91, 88, 83, 95, 86, 95, 92, 86, 90, 82])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1[cond]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "e7013076",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[55, 20, 42],\n",
       "        [ 5, 72, 57],\n",
       "        [72, 57,  6],\n",
       "        [66, 46, 68],\n",
       "        [39, 30, 48]],\n",
       "\n",
       "       [[67, 51, 22],\n",
       "        [73, 52, 53],\n",
       "        [44, 18, 62],\n",
       "        [78,  4, 88],\n",
       "        [45, 41, 94]],\n",
       "\n",
       "       [[77,  4, 93],\n",
       "        [10, 38, 94],\n",
       "        [19, 39, 96],\n",
       "        [ 3, 39, 55],\n",
       "        [89,  7,  6]],\n",
       "\n",
       "       [[86,  4, 47],\n",
       "        [58, 23,  6],\n",
       "        [47, 96, 68],\n",
       "        [48, 67, 94],\n",
       "        [73, 55, 55]]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.random.randint(0,100,size = (4,5,3,))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "6ed15c5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[47, 96, 68],\n",
       "       [10, 38, 94]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3[[-1,2],[2,1]]\n",
    "#取出最后一个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "b370de25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5, 3)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3.shape\n",
    "#知道几维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "a5cd6136",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 5, 57],\n",
       "        [66, 68]],\n",
       "\n",
       "       [[10, 94],\n",
       "        [ 3, 55]]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果打算把偶数行取出来\n",
    "arr3[::2,1::2,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "2552d15e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 28, 31, 94, 28, 33, 19, 65, 74, 91, 40, 88, 14, 61, 54, 20, 52,\n",
       "       60, 83, 32,  8,  8, 95, 23, 55, 51, 49, 23, 37, 75, 86, 95, 19, 13,\n",
       "       49, 79, 76, 78, 43, 34, 48, 19, 49,  5,  7, 92, 55,  4, 15, 63, 72,\n",
       "       86, 54, 55, 69, 90, 74, 36, 82])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([82, 36, 74, 90, 69, 55, 54, 86, 72, 63, 15,  4, 55, 92,  7,  5, 49,\n",
       "       19, 48, 34, 43, 78, 76, 79, 49, 13, 19, 95, 86, 75, 37, 23, 49, 51,\n",
       "       55, 23, 95,  8,  8, 32, 83, 60, 52, 20, 54, 61, 14, 88, 40, 91, 74,\n",
       "       65, 19, 33, 28, 94, 31, 28, 99])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1,arr1[::-1])# 进行了颠倒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "157494db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 28, 31, 94, 28, 33, 19, 65, 74, 91, 40, 88, 14, 61, 54, 20, 52,\n",
       "       60, 83, 32,  8,  8, 95, 23, 55, 51, 49, 23, 37, 75, 86, 95, 19, 13,\n",
       "       49, 79, 76, 78, 43, 34, 48, 19, 49,  5,  7, 92, 55,  4, 15, 63, 72,\n",
       "       86, 54, 55, 69, 90, 74, 36, 82])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([82, 36, 74, 90, 69, 55, 54, 86, 72, 63, 15,  4, 55, 92,  7,  5, 49,\n",
       "       19, 48, 34, 43, 78, 76, 79, 49, 13, 19, 95, 86, 75, 37, 23, 49, 51,\n",
       "       55, 23, 95,  8,  8, 32, 83, 60, 52, 20, 54, 61, 14, 88, 40, 91, 74,\n",
       "       65, 19, 33, 28, 94, 31, 28, 99])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([82, 90, 54, 63, 55,  5, 48, 78, 49, 95, 37, 51, 95, 32, 52, 61, 40,\n",
       "       65, 28, 28])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1,arr1[::-1],arr1[::-3])\n",
    "#每三个当中取一个，倒着取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ad3fb02",
   "metadata": {},
   "source": [
    "### 形状操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d0ff28",
   "metadata": {},
   "source": [
    "#### reshape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "6f14a90a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 97, 71, 86, 44, 54],\n",
       "       [43,  4, 89, 31, 46, 16],\n",
       "       [74, 99, 35, 53, 76, 40]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(0,100,size = (3,6))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "ed50e39c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 97],\n",
       "       [71, 86],\n",
       "       [44, 54],\n",
       "       [43,  4],\n",
       "       [89, 31],\n",
       "       [46, 16],\n",
       "       [74, 99],\n",
       "       [35, 53],\n",
       "       [76, 40]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(9,2)\n",
    "#但是9行3列就不行，因为数据量不够\n",
    "#(9,3)是不行的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94ad3baf",
   "metadata": {},
   "source": [
    "#### 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "126e0988",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 97, 71, 86, 44, 54],\n",
       "       [43,  4, 89, 31, 46, 16],\n",
       "       [74, 99, 35, 53, 76, 40]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行变列，列变行\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e4b97c0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 43, 74],\n",
       "       [97,  4, 99],\n",
       "       [71, 89, 35],\n",
       "       [86, 31, 53],\n",
       "       [44, 46, 76],\n",
       "       [54, 16, 40]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "a2bd1d57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[73, 97, 71],\n",
       "       [86, 44, 54],\n",
       "       [43,  4, 89],\n",
       "       [31, 46, 16],\n",
       "       [74, 99, 35],\n",
       "       [53, 76, 40]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.reshape(6,3)\n",
    "#和上面转置不一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "c23cfcdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[55, 20, 42],\n",
       "        [ 5, 72, 57],\n",
       "        [72, 57,  6],\n",
       "        [66, 46, 68],\n",
       "        [39, 30, 48]],\n",
       "\n",
       "       [[67, 51, 22],\n",
       "        [73, 52, 53],\n",
       "        [44, 18, 62],\n",
       "        [78,  4, 88],\n",
       "        [45, 41, 94]],\n",
       "\n",
       "       [[77,  4, 93],\n",
       "        [10, 38, 94],\n",
       "        [19, 39, 96],\n",
       "        [ 3, 39, 55],\n",
       "        [89,  7,  6]],\n",
       "\n",
       "       [[86,  4, 47],\n",
       "        [58, 23,  6],\n",
       "        [47, 96, 68],\n",
       "        [48, 67, 94],\n",
       "        [73, 55, 55]]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "df0b9218",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[55, 67, 77, 86],\n",
       "        [ 5, 73, 10, 58],\n",
       "        [72, 44, 19, 47],\n",
       "        [66, 78,  3, 48],\n",
       "        [39, 45, 89, 73]],\n",
       "\n",
       "       [[20, 51,  4,  4],\n",
       "        [72, 52, 38, 23],\n",
       "        [57, 18, 39, 96],\n",
       "        [46,  4, 39, 67],\n",
       "        [30, 41,  7, 55]],\n",
       "\n",
       "       [[42, 22, 93, 47],\n",
       "        [57, 53, 94,  6],\n",
       "        [ 6, 62, 96, 68],\n",
       "        [68, 88, 55, 94],\n",
       "        [48, 94,  6, 55]]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3.T\n",
    "# 默认就是第一维和最后一维进行的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "43efff1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[55, 20, 42],\n",
       "        [ 5, 72, 57],\n",
       "        [72, 57,  6],\n",
       "        [66, 46, 68],\n",
       "        [39, 30, 48]],\n",
       "\n",
       "       [[67, 51, 22],\n",
       "        [73, 52, 53],\n",
       "        [44, 18, 62],\n",
       "        [78,  4, 88],\n",
       "        [45, 41, 94]],\n",
       "\n",
       "       [[77,  4, 93],\n",
       "        [10, 38, 94],\n",
       "        [19, 39, 96],\n",
       "        [ 3, 39, 55],\n",
       "        [89,  7,  6]],\n",
       "\n",
       "       [[86,  4, 47],\n",
       "        [58, 23,  6],\n",
       "        [47, 96, 68],\n",
       "        [48, 67, 94],\n",
       "        [73, 55, 55]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[55, 67, 77, 86],\n",
       "        [ 5, 73, 10, 58],\n",
       "        [72, 44, 19, 47],\n",
       "        [66, 78,  3, 48],\n",
       "        [39, 45, 89, 73]],\n",
       "\n",
       "       [[20, 51,  4,  4],\n",
       "        [72, 52, 38, 23],\n",
       "        [57, 18, 39, 96],\n",
       "        [46,  4, 39, 67],\n",
       "        [30, 41,  7, 55]],\n",
       "\n",
       "       [[42, 22, 93, 47],\n",
       "        [57, 53, 94,  6],\n",
       "        [ 6, 62, 96, 68],\n",
       "        [68, 88, 55, 94],\n",
       "        [48, 94,  6, 55]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr3,arr3.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "ac52c5d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5, 3)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 5, 4)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr3.shape,arr3.T.shape)\n",
    "# 其实第三维和第一维进行了转置，互换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6a8c537b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "871a54f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[55, 67, 77, 86],\n",
       "        [ 5, 73, 10, 58],\n",
       "        [72, 44, 19, 47],\n",
       "        [66, 78,  3, 48],\n",
       "        [39, 45, 89, 73]],\n",
       "\n",
       "       [[20, 51,  4,  4],\n",
       "        [72, 52, 38, 23],\n",
       "        [57, 18, 39, 96],\n",
       "        [46,  4, 39, 67],\n",
       "        [30, 41,  7, 55]],\n",
       "\n",
       "       [[42, 22, 93, 47],\n",
       "        [57, 53, 94,  6],\n",
       "        [ 6, 62, 96, 68],\n",
       "        [68, 88, 55, 94],\n",
       "        [48, 94,  6, 55]]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.transpose(arr3,axes = [2,1,0])\n",
    "# transpose -  变换转置的意思 - 其实就是上面的 .T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3e38d88",
   "metadata": {},
   "source": [
    "#### 堆叠"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "e1a4610c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 65, 54,  8, 37, 79, 49, 63, 74])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4 = arr1[::7] #每隔7个取1个\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "3205e6ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 74, 52, 55, 19, 48, 15, 74])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr5 = arr1[::8]\n",
    "arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "0386d8c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([99, 65, 54,  8, 37, 79, 49, 63, 74, 99, 65, 54,  8, 37, 79, 49, 63,\n",
       "       74, 99, 65, 54,  8, 37, 79, 49, 63, 74, 99, 74, 52, 55, 19, 48, 15,\n",
       "       74])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr4,arr4,arr4,arr5])\n",
    "#也能 执行合并\n",
    "#级联，数据增长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "8e8436ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 9],\n",
       "       [8, 7, 5],\n",
       "       [8, 0, 2],\n",
       "       [5, 3, 9],\n",
       "       [3, 6, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 1],\n",
       "       [6, 9, 5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[8, 5],\n",
       "       [0, 4],\n",
       "       [8, 1],\n",
       "       [2, 6],\n",
       "       [8, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[6, 1, 4, 3],\n",
       "       [9, 1, 8, 4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.random.randint(0,10, size = (5,3))\n",
    "arr2 = np.random.randint(0,10, size = (2,3))\n",
    "arr3 = np.random.randint(0,10, size = (5,2))\n",
    "arr4 = np.random.randint(0,10, size = (2,4))\n",
    "display(arr1,arr2,arr3,arr4)\n",
    "# 不对应是没法合并的，比如 arr1和arr4\n",
    "# 数据维度不合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "6522209b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 2, 3],\n",
       "       [7, 3, 2],\n",
       "       [5, 0, 9],\n",
       "       [6, 9, 4],\n",
       "       [3, 5, 5],\n",
       "       [9, 3, 1],\n",
       "       [2, 1, 9]])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr1,arr2],axis = 0)# 0表示 第一维合并\n",
    "# 也是可以级联的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "26dd5b59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 8, 9, 8, 5],\n",
       "       [8, 7, 5, 0, 4],\n",
       "       [8, 0, 2, 8, 1],\n",
       "       [5, 3, 9, 2, 6],\n",
       "       [3, 6, 7, 8, 2]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([arr1,arr3],axis = 1)# -1写成 1都是一回事\n",
    "#数据在第二个维度上，进行了合并。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "5ce81ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 3)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(5, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1.shape,arr3.shape)\n",
    "# 其实就是第二维 3+2=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50f41f78",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
